import {
  areaList
} from '../../utils/areaData';
const api = require('../../api/index'); // 头部引入

// pages/addOrEditAddress/addOrEditAddress.js
Page({
  data: {
    areaList: {
      province_list: areaList.province_list, //省
      city_list: areaList.city_list, //市
      county_list: areaList.county_list //区
    },
    address: "",
    detailAddress: "",
    userName: "",
    phone: "",
    bqList: [{
      title: "家",
      label: "2"
    }, {
      title: "公司",
      label: "1"
    }, {
      title: "学校",
      label: "3"
    }],
    label: "2",
    showAddress: false,
    sex: "0",
    deleteBtn: false,
    addressId: null,
    addressBook: {},
    errorPhone: "",
    isDefault: false
  },
  //弹出地址选择框
  onShowAddress() {
    this.setData({
      showAddress: true
    })
  },
  //地址选择框确认
  onConfirm(e) {
    console.log(e);
    this.setData({
      address: e.detail.values[0].name + e.detail.values[1].name + e.detail.values[2].name,
      'addressBook.provinceName': e.detail.values[0].name,
      'addressBook.provinceCode': e.detail.values[0].code,
      'addressBook.cityName': e.detail.values[1].name,
      'addressBook.cityCode': e.detail.values[1].code,
      'addressBook.districtName': e.detail.values[2].name,
      'addressBook.districtCode': e.detail.values[2].code,
      showAddress: false
    })
  },
  //地址选择框取消
  onCancel(e) {
    this.setData({
      showAddress: false
    })
  },
  //地址选择框关闭
  onClose(e) {
    this.setData({
      showAddress: false
    })
  },
  //选择标签
  onSelectBQ(e) {
    this.setData({
      //拿到当前索引并动态改变
      label: e.currentTarget.dataset.label,
      'addressBook.label': e.currentTarget.dataset.label,
    })
  },
  //选择男女士
  onChangeSex(event) {
    console.log(event);
    this.setData({
      sex: event.detail,
      'addressBook.sex': event.detail,
    });
  },
  //输入详细地址时触发
  onInputAddress(event) {
    this.setData({
      detailAddress: event.detail
    })
  },
  //清空详细地址
  onClearAddress(event) {
    this.setData({
      detailAddress: "",
    })
  },
  //输入姓名时触发
  onInputuserName(event) {
    this.setData({
      userName: event.detail
    })
  },
  //清空姓名
  onClearuserName(event) {
    this.setData({
      userName: "",
    })
  },
  //输入手机号时触发
  onInputPhone(event) {
    this.setData({
      phone: event.detail
    })
  },
  //清空手机号
  onClearPhone(event) {
    this.setData({
      phone: "",
      errorPhone: "",
    })
  },
  //设置默认地址
  onChangeDefault() {
    console.log(this.data.isDefault);
    this.setData({
      isDefault: !this.data.isDefault,
    })
  },
  //保存地址
  onAddOrEditAddress() {
    let data = this.data.addressBook;
    data.detail = this.data.detailAddress;
    data.consignee = this.data.userName;
    data.label = this.data.label;
    data.phone = this.data.phone;
    data.sex = this.data.sex;
    data.isDefault = this.data.isDefault == true ? 1 : 0;
    console.log(data);
    let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    let phone = this.data.phone;
    if (!reg_tel.test(phone)) {
      this.setData({
        errorPhone: "请输入正确的手机号！"
      })
      return;
    } else {
      this.setData({
        errorPhone: ""
      })
    }
    //deleteBtn为true:修改地址
    if (this.data.deleteBtn) {
      console.log(1);
      api.updataAddress(data).then(res => {
        if (res.code == 1) {
          this.getPage() //调用获取页面栈方法
          //返回上一页
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      })
    } else { //deleteBtn为false:为添加地址
      console.log(2);
      api.addAddress(data).then(res => {
        if (res.code == 1) {
          this.getPage() //调用获取页面栈方法
          //返回上一页
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      })
    }
  },
  //删除地址
  onDeleteAddress() {
    console.log(this.data.addressId);
    api.deleteAddress(this.data.addressId).then(res => {
      if (res.code == 1) {
        this.getPage() //调用获取页面栈方法
        //返回上一页
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  //获取页面栈方法
  getPage() {
    let pages = getCurrentPages(); // 获取页面栈
    let currPage = pages[pages.length - 1]; // 当前页面
    let prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.getAddressList()
  },
  //根据id获取地址
  getAddressById() {
    api.getAddressById(this.data.addressId).then(res => {
      // console.log(res.data);
      if (res.code == 1) {
        this.setData({
          addressBook: res.data,
          address: res.data.provinceName + res.data.cityName + res.data.districtName,
          detailAddress: res.data.detail,
          userName: res.data.consignee,
          sex: res.data.sex,
          phone: res.data.phone,
          label: res.data.label,
          isDefault: res.data.isDefault == 1 ? true : false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.addressId);
    if (options.addressId != undefined) {
      this.setData({
        addressId: JSON.parse(options.addressId),
        deleteBtn: true,
      });

      this.getAddressById()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    if (this.data.deleteBtn) {
      wx.setNavigationBarTitle({
        title: "编辑收货地址"
      })
    } else {
      wx.setNavigationBarTitle({
        title: "新增收货地址"
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})